# -*- coding: utf-8 -*-
# @Author  : wyh
# @Time    : 2023/2/12 15:19
# @File    : load
# @Email   : 2448979539@qq.com
from pathlib import Path
from django.contrib.gis.utils import LayerMapping
from world.models import WorldBorder
import world

# 模型字段和Layer对象 映射
world_mapping = {
    'fips': 'FIPS',
    'iso2': 'ISO2',
    'iso3': 'ISO3',
    'un': 'UN',
    'name': 'NAME',
    'area': 'AREA',
    'pop2005': 'POP2005',
    'region': 'REGION',
    'subregion': 'SUBREGION',
    'lon': 'LON',
    'lat': 'LAT',
    'mpoly': 'MULTIPOLYGON',
}

world_shp = Path(__file__).resolve().parent / 'data' / 'TM_WORLD_BORDERS-0.3.shp'


def run(verbose=True):
    # 几何映射
    layer_mapping = LayerMapping(model=WorldBorder, data=world_shp, mapping=world_mapping, transform=False)
    # strict严格模式, verbose详细日志
    layer_mapping.save(strict=True, verbose=verbose)
